package com.cdtu.outtake.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.cdtu.outtake.common.Result;
import com.cdtu.outtake.domain.User;
import com.cdtu.outtake.dto.ShopDto;
import com.cdtu.outtake.service.IndexService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * @Author : shl
 * @Date 2024/3/20 19:04
 */
@RestController
@RequestMapping("/index")
@RequiredArgsConstructor
public class IndexController {

    private final IndexService indexService;

    @GetMapping()
    public Result<List<ShopDto>> index(@RequestParam("dineIn") boolean dineIn,
                                       @RequestParam("takeaway") boolean takeaway,
                                       @RequestParam("offlinePickUp") boolean offlinePickUp,
                                       @RequestParam("orderType") String orderType) {
        if (!StpUtil.isLogin()) {
            return Result.ok(indexService.getShops(null, dineIn, takeaway, offlinePickUp, orderType));
        }
        Long schoolId = Optional.of((User) StpUtil.getSession().get("userInfo", null))
                .map(User::getSchoolId)
                .orElse(null);
        return Result.ok(indexService.getShops(schoolId, dineIn, takeaway, offlinePickUp, orderType));
    }
}
